﻿local _, ns = ...
--名字
function ns.CteatNewPlateName(unitFrame)
	if not unitFrame then
		for i, namePlate in ipairs(C_NamePlate.GetNamePlates()) do
			ns.CteatNewPlateName(namePlate.UnitFrame)
		end
	end

	if not unitFrame then return end
	if not unitFrame.unit then return end
	if unitFrame:IsForbidden() then return end
	
	if not unitFrame.nametext then
		unitFrame.nametext = unitFrame:CreateFontString(nil, "ARTWORK")
	end
	
	if not unitFrame.nametext then return end
	
	--unitFrame.name:SetText("            ")
	unitFrame.name:SetAlpha(0)
	
	if PlateColorDB.showAllName and not UnitIsUnit(unitFrame.unit,"player") then
		unitFrame.nametext:Show()
	elseif ShouldShowName(unitFrame) and not UnitIsUnit(unitFrame.unit,"player") then
		unitFrame.nametext:Show()
	else
		unitFrame.nametext:Hide()
	end
	
	local guid = UnitGUID(unitFrame.unit)
	local _, _, _, _, _, id = strsplit("-", guid or "") 
	local namecolor = type(PlateColorDB["NPCs"][id]) == "table" and PlateColorDB["NPCs"][id]["color"]
	
	unitFrame.nametext:SetPoint("BOTTOM",unitFrame.healthBar,"TOP",0,2)
	

	if type(PlateColorDB["NPCs"][id]) == "table" and PlateColorDB["NPCs"][id]["uselarge"] then
		unitFrame.nametext:SetFont(unitFrame.name:GetFont(), PlateColorDB.enemyNameScale*2.4, PlateColorDB.enemyNameOUTLINE and "OUTLINE" or "")
	elseif UnitCanAttack("player",unitFrame.unit)  then
		unitFrame.nametext:SetFont(unitFrame.name:GetFont(), PlateColorDB.enemyNameScale*1.4, PlateColorDB.enemyNameOUTLINE and "OUTLINE" or "")
	elseif UnitIsPlayer(unitFrame.unit) then
		unitFrame.nametext:SetFont(unitFrame.name:GetFont(), PlateColorDB.friendNameScale*1.4, PlateColorDB.friendNameOUTLINE and "OUTLINE" or "")
	else
		unitFrame.nametext:SetFont(unitFrame.name:GetFont(), PlateColorDB.NPCNameScale*1.3, PlateColorDB.NPCNameOUTLINE and "OUTLINE" or "")
	end

	if UnitIsPlayer(unitFrame.unit) and not UnitCanAttack(unitFrame.unit,"player") then--友方玩家职业颜色
		local _,nameplateclass = UnitClass(unitFrame.unit)
		if nameplateclass then
			local color = CUSTOM_CLASS_COLORS and CUSTOM_CLASS_COLORS[nameplateclass] or RAID_CLASS_COLORS[nameplateclass]
			unitFrame.nametext:SetVertexColor(color.r,color.g,color.b)
		end
		
	elseif unitFrame.useNameColor and unitFrame.useNameColor.spellId and PlateColorDB["Auras"][unitFrame.useNameColor.spellId] and
	PlateColorDB["Auras"][unitFrame.useNameColor.spellId]["usename"] and
	(unitFrame.useNameColor.applications >= PlateColorDB["Auras"][unitFrame.useNameColor.spellId]["stack"] or unitFrame.useNameColor.applications == 0) then--光环变色
		local DB = PlateColorDB
		local hpcid = unitFrame.useNameColor.spellId
		unitFrame.nametext:SetVertexColor(DB["Auras"][hpcid]["color"]["r"],DB["Auras"][hpcid]["color"]["g"],DB["Auras"][hpcid]["color"]["b"])
		
	elseif type(PlateColorDB["NPCs"][id]) == "table" and PlateColorDB["NPCs"][id]["usename"] then--NPC颜色
		unitFrame.nametext:SetVertexColor(namecolor["r"],namecolor["g"],namecolor["b"])
		
	elseif ( CompactUnitFrame_IsTapDenied(unitFrame) or (UnitIsDead(unitFrame.unit) and not UnitIsPlayer(unitFrame.unit))) then
	-- Use grey if not a player and can't get tap on unit
		unitFrame.nametext:SetVertexColor(0.5, 0.5, 0.5);
		
	elseif CompactUnitFrame_IsOnThreatListWithPlayer(unitFrame.displayedUnit) then
		if PlateColorDB.whiteName then
			unitFrame.nametext:SetVertexColor(1.0, 1.0, 1.0);
		else
			unitFrame.nametext:SetVertexColor(1.0, 0.0, 0.0);
		end
		
	elseif (UnitReaction(unitFrame.unit, "player") and UnitReaction(unitFrame.unit, "player")~=0) and UnitReaction(unitFrame.unit, "player") == 4 then
		unitFrame.nametext:SetVertexColor(1, 1, 0);
		
	elseif (UnitReaction(unitFrame.unit, "player") and UnitReaction(unitFrame.unit, "player")~=0) and UnitReaction(unitFrame.unit, "player") >= 5 then
		unitFrame.nametext:SetVertexColor(0, 1, 0);
		
	elseif PlateColorDB.whiteName then
		unitFrame.nametext:SetVertexColor(1.0, 1.0, 1.0);
		
	else
		unitFrame.nametext:SetVertexColor(1.0, 0.0, 0.0);
	end
	
	unitFrame.nametext:SetText(UnitName(unitFrame.unit))
	if unitFrame.nametext and ns.sb[UnitGUID(unitFrame.unit)] then
		unitFrame.nametext:SetText(ns.sb[UnitGUID(unitFrame.unit)]) 
	end
	
	if not unitFrame.NpcFuntext then
		unitFrame.NpcFuntext = unitFrame:CreateFontString(nil, "ARTWORK")
		unitFrame.NpcFuntext:SetPoint("TOP",unitFrame.nametext,"BOTTOM",0,0)
		unitFrame.NpcFuntext:Hide()
	end
	if unitFrame.NpcFuntext then
		unitFrame.NpcFuntext:SetFont(ns.fonts, PlateColorDB.NPCNameScale*1.1, PlateColorDB.NPCNameOUTLINE and "OUTLINE" or "")
		if PlateColorDB.NPCwhiteFunText then
			unitFrame.NpcFuntext:SetVertexColor(1,1,1)
		else
			unitFrame.NpcFuntext:SetVertexColor(unitFrame.nametext:GetVertexColor())
		end
		unitFrame.NpcFuntext:SetAlpha(0.9)
		if unitFrame.nametext:IsShown() and not UnitIsPlayer(unitFrame.unit) and (not unitFrame.healthBar:IsShown() or unitFrame.healthBar:GetWidth() < 1) then
			unitFrame.NpcFuntext:Show()
		else
			unitFrame.NpcFuntext:Hide()
		end
		
		local tooltipData = C_TooltipInfo.GetUnit(unitFrame.unit)
		if not tooltipData then return end
		if tooltipData.lines[2] and not string.match(tooltipData.lines[2].leftText,LEVEL) then
			unitFrame.NpcFuntext:SetText(tooltipData.lines[2].leftText)
		else
			unitFrame.NpcFuntext:SetText("")
		end
	end

end


--系统友方名字
function ns.SetBlzPlateName()
	local namevalue = PlateColorDB.friendNameScale
	local fontline = PlateColorDB.friendNameOUTLINE and "OUTLINE" or ""
	local function SetFont(obj, optSize) 
		local fontName, _,fontFlags  = obj:GetFont() 
		obj:SetFont(fontName,optSize,fontline) 
		obj:SetShadowOffset(0, 0) 
	end
	SetFont(SystemFont_LargeNamePlate,namevalue)
	SetFont(SystemFont_NamePlate,namevalue) 
	SetFont(SystemFont_LargeNamePlateFixed,namevalue) 
	SetFont(SystemFont_NamePlateFixed,namevalue) 
	SetFont(SystemFont_NamePlateCastBar,namevalue)
end

--更新名字时更新
hooksecurefunc("CompactUnitFrame_UpdateName", function(unitFrame)
	if unitFrame:IsForbidden() then return end
	if unitFrame.unit and ns.sb[UnitGUID(unitFrame.unit)] and unitFrame.name then unitFrame.name:SetText(ns.sb[UnitGUID(unitFrame.unit)]) end
	if not string.match(unitFrame.unit,"nameplate") then return end
	ns.CteatNewPlateName(unitFrame)
end)

local MyNameEvent = CreateFrame("Frame")
MyNameEvent:RegisterEvent("NAME_PLATE_UNIT_ADDED")
MyNameEvent:SetScript("OnEvent", function(self, event, ...)
	ns.SetBlzPlateName()
end)